home *** CD-ROM | disk | FTP | other *** search
- /***
- * URLHLibrary.c
- *
- * Library of interface routines to make life simpler when trying to
- * access the URLH component.
- *
- * This library assumes that global data is an ok thing to use. If
- * you don't want to use it, then hack this puppy apart and
- * cut/paste what parts you need.
- *
- * HEY -- This file assumes that a parseref points to valid memory.
- * This is certianly true if the current implementation, and will be
- * for at least a while. Things might well break if it isn't... Perhaps
- * I should fix that before this goes production!
- ***/
-
- #include "URLHelperComponent.h"
- #include "URLHLibrary.h"
-
- URLHelperComponent urlInstance = 0;
-
- typedef struct {
- long check; // Hold a hash number so we can id this guy
- Str255 url; // Name of failed url
- } bumParseRef, *bumParseRefPtr;
-
- #define CHECK_ID 0x2a45f8ab
-
- /**
- * URHLibOpen
- *
- * Open up the component -- return an error if we can't
- *
- **/
- OSErr URLHLibOpen (void)
- {
- if (urlInstance == 0)
- urlInstance = OpenDefaultComponent (URLHelperComponentType, 0L);
-
- if (urlInstance == 0)
- return -1;
-
- return noErr;
- }
-
- /**
- * URLHLibClose
- *
- * Close the component, if it is open!
- *
- **/
- void URLHLibClose (void)
- {
- if (urlInstance)
- CloseComponent (urlInstance);
- urlInstance = 0;
- }
-
- /**
- * URLHLibUse
- *
- * Used if you want to use a particular instance of the component.
- *
- **/
- void URLHLibUse (URLHelperComponent instance)
- {
- URLHLibClose ();
- urlInstance = instance;
- }
-
- /**
- * URLHLibGetFirstMirror
- *
- * Return the first mirror we find for the given url. Return the url
- * if there is no URLHelper component on the system or if we can't
- * find the damm url in our mirror list.
- *
- * It is ok for the baseURL and mirrorURL to point to the same memory.
- * This routine should always return noErr...
- *
- **/
- void URLHLibGetFirstMirror (StringPtr baseURL, StringPtr mirrorURL)
- {
- OSErr theErr;
- URLHParseRef parseRef;
- Boolean disposeParseState;
-
- /**
- ** First, check to see if we can open up the url component...
- **/
-
- theErr = URLHLibOpen ();
-
- /**
- ** Now, parse the darn thing
- **/
-
- if (theErr == noErr)
- theErr = URLHNewParseState (urlInstance, baseURL, &parseRef);
- disposeParseState = theErr == noErr;
-
- /**
- ** And get back the first mirror we can
- **/
-
- if (theErr == noErr)
- theErr = URLHGetMirrorRep (urlInstance, parseRef, mirrorURL, 1);
-
- if (disposeParseState)
- URLHDisposeParseState (urlInstance, parseRef);
-
- /**
- ** If there has been an error, then copy over the old url into the new url...
- **/
-
- if (theErr != noErr
- && baseURL != mirrorURL)
- BlockMove (baseURL, mirrorURL, baseURL[0]+1);
-
-
- return;
- }
-
- /**
- ** The next three routines allow one to access all the mirrors in the list, one by one.
- ** The caller must keep track of a parse state variable. These routines are designed to
- ** work weather or not the component is present on the system.
- **/
-
- /**
- * URLHLibNewParseState
- *
- * This routine will create a new parse state. Pass in the baseURL and get back the
- * parse state.
- *
- **/
- URLHParseRef URLHLibNewParseState (StringPtr baseURL)
- {
- OSErr theErr;
- URLHParseRef parseRef;
- bumParseRefPtr bummer;
-
- theErr = URLHLibOpen ();
-
- if (theErr == noErr)
- theErr = URLHNewParseState (urlInstance, baseURL, &parseRef);
-
- if (theErr == noErr)
- return parseRef;
-
- /**
- ** Bummer. We didn't do it. Create our own struct so we can communicate this
- ** info back to the next set of routines...
- **/
-
- bummer = (bumParseRefPtr) NewPtrClear (sizeof (bumParseRef));
- bummer->check = CHECK_ID;
- BlockMove (baseURL, bummer->url, baseURL[0]+1);
-
- return (URLHParseRef) bummer;
- }
-
- /**
- * URLHLibGetIndexedMirror
- *
- * Return (from a parse state gotten from LibNewParseState) the index'edth mirror. OSErr
- * will be -1 when we have looped through all the mirrors.
- *
- **/
- Boolean URLHLibGetIndexedMirror (URLHParseRef parseRef, short index, StringPtr mirrorURL)
- {
- OSErr theErr;
- bumParseRefPtr bummer;
-
- mirrorURL[0] = 0;
-
- /**
- ** Check to see if we have a bum parse ref pointer...
- **/
-
- bummer = (bumParseRefPtr) parseRef;
- if (bummer->check == CHECK_ID) {
- if (index != 1)
- return false;
- BlockMove (bummer->url, mirrorURL, bummer->url[0]+1);
- return true;
- }
-
- /**
- ** Ok -- normal url. deal with it!
- **/
-
- theErr = URLHGetMirrorRep (urlInstance, parseRef, mirrorURL, index);
- if (theErr != noErr)
- return false;
-
- return true;
- }
-
- /**
- * URLHLibDisposeParseState
- *
- * This guy will get rid of an old parse ref
- *
- **/
- void URLHLibDisposeParseState (URLHParseRef parseRef)
- {
- bumParseRefPtr bummer;
-
- /**
- ** Make sure it isn't one of our bummer guys...
- **/
-
- bummer = (bumParseRefPtr) parseRef;
- if (bummer->check == CHECK_ID) {
- DisposPtr ((Ptr) bummer);
- return;
- }
-
- URLHDisposeParseState (urlInstance, parseRef);
- return;
-
- }